﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class UpdateRole : SOSecurityPage
    {
        Facade.StructureFacade structureFacade = new Facade.StructureFacade();

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidateUserCredentials(Request.Url.LocalPath);

            if (!IsPostBack)
            {

                if (Page.Request.Params["RoleId"] != null)
                {
                    UserRole_aspnet asp_role = structureFacade.GetAspnetUserRole(new Guid(Page.Request.Params["RoleId"]));
                    txtNewRole.Text = asp_role.RoleName;

                    UserRole role = structureFacade.GetUserRole(asp_role.RoleId);
                    UserType userType = structureFacade.GetUserType(role.UserTypeId);
                    if (role.Status == 1)
                        chkEnabled.Checked = true;

                    SetDataSourceToTypes(structureFacade.GetUserTypes(""),ddlUserType);
                    SetDataSourceToTypes(structureFacade.GetUserTypes(""), ddlNewUserType);

                    ddlNewUserType.SelectedValue = role.UserTypeId.ToString();
                    ddlUserType.SelectedValue = role.UserTypeId.ToString();
                    SetDataSourceToRoles(structureFacade.GetAspnetUserRoleForUserType(Convert.ToInt32(ddlUserType.SelectedValue)));
                    ddlRole.SelectedValue = role.RoleId.ToString();
                }
                else
                {
                    SetDataSourceToTypes(structureFacade.GetUserTypes(""),ddlUserType);
                }
            }
        }

        public void SetDataSourceToTypes(List<UserType> userTypes,DropDownList ddl)
        {

            ddl.DataSource = userTypes;
            ddl.DataTextField = "UserTypeName";
            ddl.DataValueField = "UserTypeId";
            ddl.DataBind();


            InsertDDLItems(ddl, "Select...", 0);
            //InsertDDLItems(ddlFrom, "Select All", 1);
        }


        protected void ddlRoleType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlUserType.SelectedIndex > 0)
                SetDataSourceToRoles(structureFacade.GetAspnetUserRoleForUserType(Convert.ToInt32(ddlUserType.SelectedValue)));
        }

        protected void ddlNewUserType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlRole.SelectedIndex > 0)
            {
                UserRole_aspnet asp_role = structureFacade.GetAspnetUserRole(new Guid(ddlRole.SelectedValue));
                txtNewRole.Text = asp_role.RoleName;

                UserRole role = structureFacade.GetUserRole(asp_role.RoleId);
                UserType userType = structureFacade.GetUserType(role.UserTypeId);
                if (role.Status == 1)
                    chkEnabled.Checked = true;

                SetDataSourceToTypes(structureFacade.GetUserTypes(""), ddlNewUserType);
                ddlNewUserType.SelectedValue = role.UserTypeId.ToString();
            }
        }

        public void SetDataSourceToRoles(List<UserRole_aspnet> userRoles)
        {
            ddlRole.DataSource = userRoles;
            ddlRole.DataTextField = "RoleName";
            ddlRole.DataValueField = "RoleId";
            ddlRole.DataBind();

            InsertDDLItems(ddlRole, "Select...", 0);
            //InsertDDLItems(ddlRoles, "Select All", 1);
        }

        public void InsertDDLItems(DropDownList ddl, string Text, int Position)
        {
            //ListItem item = new ListItem(Text);
            //ddl.Items.Insert(Position, item);
            ddl.Items.Insert(Position, Text);
        }

        protected void btnUpdateRole_Click(object sender, EventArgs e)
        {
            UserRole role = new UserRole();

            role.RoleId =new Guid(ddlRole.SelectedValue);
            role.UserTypeId = Convert.ToInt16(ddlNewUserType.SelectedValue);
            if (chkEnabled.Checked)
                role.Status = 1;
            else
                role.Status = 0;

            structureFacade.UpdateUserRole(role);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/UserManagement/ViewRole.aspx");
        }
    }
}
